<template>
  <div>
    <el-page-header @back="goBack" :content="$t('common.Edit')">
    </el-page-header>
  <div class="columns">
    <div class="column is-full">
      <el-card class="box-card" shadow="never">
        <div slot="header" class="clearfix">
          <span style="font-size:16px">{{$t('common.blogPost')}}</span>
        </div>
        <div>
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            :rules="rules"
            class="demo-ruleForm"
          >
            <el-form-item prop="title">
              <el-input
                v-model="ruleForm.title"
                :placeholder="$t('common.title')"
              />
            </el-form-item>
            
            <EDITOR v-model="editorContent"></EDITOR>

            <b-taginput
              v-model="ruleForm.tags"
              class="my-3"
              maxlength="15"
              maxtags="3"
              ellipsis
              :placeholder="$t('message.addTags')"
            />

            <el-form-item>
              <el-button
                type="primary"
                @click="submitForm('ruleForm')"
              >{{$t('common.create')}}
              </el-button>
              <el-button @click="resetForm('ruleForm')">{{$t('common.reset')}}</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
  </div>
  </div>
</template>

<script>
import { post } from '@/api/post'
import 'vditor/dist/index.css'
import editor from '@/components/editor/editor'
export default {
  name: 'TopicPost',
  components: {
      EDITOR: editor,
  },
  data() {
    return {
      editorContent:'',
      ruleForm: {
        title: '', 
        tags: [], 
        content: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          {
            min: 1,
            max: 25,
            message: '长度在 1 到 25 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (
            this.editorContent == null ||
            this.editorContent == ''
          ) {
            alert('文章内容不可以为空')
            return false
          }
          if (this.ruleForm.tags == null || this.ruleForm.tags.length === 0) {
            alert('标签不可以为空')
            return false
          }
          this.ruleForm.content = this.editorContent
          post(this.ruleForm).then((response) => {
            const { data } = response
            setTimeout(() => {
              this.$router.push({
                name: 'post-detail',
                params: { id: data.id }
              })
            }, 800)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.editorContent = ''
      this.ruleForm.tags = ''
    },
    goBack(){
        this.$router.push("/")
    }
  }
}
</script>

<style>
  
</style>